﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace Accounts
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private CollectionViewSource _accountViewSource;
        private SalesManagerEntities _sme;
        private int _ownerID;

        public MainWindow()
        {
            InitializeComponent();

            _sme = new SalesManagerEntities();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            int.TryParse(App.Args[0], out _ownerID);

            ((CollectionViewSource)FindResource("AccountTypeViewSource")).Source =
                from c in _sme.codeAccountType select c;
            ((CollectionViewSource)FindResource("CurrencyViewSource")).Source =
                from c in _sme.codeCurrency select c;
            _accountViewSource = (CollectionViewSource)FindResource("AccountViewSource");
            LoadList();
        }

        private void LoadList()
        {
            if (_accountViewSource == null)
                return;
            if ((bool)btnGlobalAccounts.IsChecked)
            {
                _accountViewSource.Source = from c in _sme.tblAccount
                                            where c.OwnerID == null
                                               && c.Name != null
                                            select c;
            }
            else
            {
                _accountViewSource.Source = from c in _sme.tblAccount
                                            where c.OwnerID == _ownerID
                                               && c.Name != null
                                            select c;
            }
        }

        private void btnGlobalAccounts_Checked(object sender, RoutedEventArgs e)
        {
            LoadList();
        }

        private void btnAccountHistory_Click(object sender, RoutedEventArgs e)
        {
            TransactionHistory dlg = new TransactionHistory(_sme, (tblAccount)_accountViewSource.View.CurrentItem);
            dlg.ShowDialog();
        }

        private void brnSave_Click(object sender, RoutedEventArgs e)
        {
            _sme.SaveChanges();
        }

        private void btnNew_Click(object sender, RoutedEventArgs e)
        {
            tblAccount newAccount = (tblAccount)((IEditableCollectionView)_accountViewSource.View).AddNew();
            _sme.AddTotblAccount(newAccount);
            newAccount.Name = "New Account";
            newAccount.CreateDate = DateTime.Now;
            newAccount.CurrencyID = 1;
            newAccount.AccountTypeCode = "E";
            //newAccount.Currency = (codeCurrency)_sme.GetObjectByKey(new System.Data.EntityKey("SalesManagerEntities.codeCurrency", "CurrencyID", 1));
            //newAccount.AccountType = (codeAccountType)_sme.GetObjectByKey(new System.Data.EntityKey("SalesManagerEntities.codeAccountType", "AccountTypeCode", "E"));
            if (!(bool)btnGlobalAccounts.IsChecked)
                newAccount.OwnerID = _ownerID;
            _accountViewSource.View.MoveCurrentTo(newAccount);
            txtName.Focus();
            txtName.SelectAll();
        }
    }
}
